package gui;

import gui.GUIConstants.PopupSize;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.event.MouseInputAdapter;


import core.Constants;
import core.Core;
import core.UpdateListener;


/**
 * 
 * @author Anton
 *
 * The panel containing the shown map.
 */
@SuppressWarnings("serial")
public class CenterPanel extends JPanel implements UpdateListener{
	private static CenterPanel me;
	
	private List<AnimationInterface> animations = new ArrayList<AnimationInterface>();
	
	public static CenterPanel getInstance(){
		if(me == null)
			me = new CenterPanel();
		return me;
	}

	private CenterPanel(){
		MouseInputAdapter mouseListener = new CenterPanelListener();
		addMouseListener(mouseListener);
		addMouseMotionListener(mouseListener);
		Core.getInstance().addUpdateListener(this);
		GUICore.getInstance().addUpdateListener(this);
		AnimationController.getInstance().addUpdateListener(this);
	}
	
	public void paintComponent(Graphics g){
		switch(GUICore.getInstance().currentView){
		case MAP:
			paintMap(g);
			break;
		case BATTLE:
			paintBattle(g);
			break;
		}
		paintBorder(g);
	}
	public void paintMap(Graphics g){
		g.setColor(Color.black);
		g.fillRect(0, 0, 1300, 1000);
		try{
		for(int x=0; x<Constants.MAPSIZE; x++){
			for(int y=0; y<Constants.MAPSIZE; y++){
				//guiMap[x][y].paint(g, (x+1)*GUIConstants.TILESIZE, (y+1)*GUIConstants.TILESIZE);
				GUICore.getInstance().getGUIMap()[x][y].paint(g);
			}
		}
		for(SelectionSphere s : GUICore.getInstance().selectionSpheres){
			s.paint(g);
		}
		}
		catch (NullPointerException ne){
			ne.printStackTrace();
		}
		for(AnimationInterface a : animations)
			a.paintComponent(g);
		paintSelectedBar(g);
		paintEffectPopupInfo(g);
	}
	public void paintBattle(Graphics g){
		BattleView.getInstance().paint(g);
		
	}
	public void paintBorder(Graphics g){
		for(int i = 0; i<=this.getWidth()/50; i++)
			ImageHandler.getInstance().drawImage(i*50, getHeight()-20, 50, 20, "boarderThickFinal1.jpg", g);
	}
	public void paintSelectedBar(Graphics g){
		SelectedBar.getInstance().paint(g);
	}
	private void paintEffectPopupInfo(Graphics g) {
		if(EffectPopupInfo.getInstance().getName() != null)
			EffectPopupInfo.getInstance().paint(g, this.getHeight());
	}
	public void addAnimation(AnimationInterface a){
		animations.add(a);
	}
	
	public List<AnimationInterface> getAnimations(){
		return animations;
	}
	public void update(){
		repaint();
	}
}
